package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateDeviceWhiteListAuthRulesDTO;
import com.itlong.cloud.POJO.PO.DeviceWhiteListIAuthFileRulesInfoPO;
import com.itlong.cloud.POJO.VO.operate.OperateDeviceWhiteListAuthRulesVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateDeviceWhiteListAuthRulesDao;
import com.itlong.cloud.operate.service.IOperateDeviceWhiteListAuthRulesService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.encrypt.UserTokenUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-设备白名单授权文件规则管理服务接口实现类
 * </desc>
 *
 * @createDate 2020/08/11
 **/
@Service
public class OperateDeviceWhiteListAuthRulesServiceImpl implements IOperateDeviceWhiteListAuthRulesService {

    @Autowired
    private IOperateDeviceWhiteListAuthRulesDao iOperateDeviceWhiteListAuthRulesDao;

    /**
     * <desc>
     *      分页获取设备白名单授权文件规则信息
     * </desc>
     *
     * @param dto 设备白名单DTO
     * @return
     * @Author Juguang.S
     * @createDate  2020/08/11
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateDeviceWhiteListAuthRulesVO> getWhiteListAuthRulesByPage(OperateDeviceWhiteListAuthRulesDTO dto) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        Page<OperateDeviceWhiteListAuthRulesVO> page = new Page<>();
        page.getPage().setPageSize(dto.getPageSize());
        page.getPage().setCurrentPage(dto.getCurrentPage());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        List<OperateDeviceWhiteListAuthRulesVO> whiteListAuthRulesByPage = iOperateDeviceWhiteListAuthRulesDao.getWhiteListAuthRulesByPage(params);
        for(OperateDeviceWhiteListAuthRulesVO authRulesVO : whiteListAuthRulesByPage){
            authRulesVO.setPassword(UserTokenUtil.authFilePsdDecrypt(authRulesVO.getPassword(),PlatformConstants.OPERATE_USER_PASSWORD_KEY));
        }
        page.setRows(whiteListAuthRulesByPage,iOperateDeviceWhiteListAuthRulesDao.getWhiteListAuthRulesByPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      保存设备白名单授权文件规则信息
     * </desc>
     *
     * @param dto 设备白名单授权文件规则DTO
     * @return
     * @Author Juguang.S
     * @createDate  2020/08/11
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void save(OperateDeviceWhiteListAuthRulesDTO dto) throws Exception {
        Date nowTime = new Date();
        DeviceWhiteListIAuthFileRulesInfoPO authFileRulesInfoPO = new DeviceWhiteListIAuthFileRulesInfoPO();
        BeanUtils.copyProperties(dto,authFileRulesInfoPO);
        authFileRulesInfoPO.setRuleId(LogicIdUtil.bussinessId());
        authFileRulesInfoPO.setCreateTime(nowTime);
        authFileRulesInfoPO.setCreateUserId(dto.getLoginUserId());
        authFileRulesInfoPO.setOperateUserId(dto.getLoginUserId());
        authFileRulesInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        authFileRulesInfoPO.setStartTime(dateFormat.parse(dto.getStartTime()));
        authFileRulesInfoPO.setEndTime(dateFormat.parse(dto.getEndTime()));
        authFileRulesInfoPO.setUsedTimes(0);
        iOperateDeviceWhiteListAuthRulesDao.save(SqlUtil.durableData(authFileRulesInfoPO, PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      禁用启用设备白名单授权文件规则
     * </desc>
     *
     * @param dto 参数DTO
     * @return
     * @Author Juguang.S
     * @createDate  2020/08/11
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void forbidden(OperateDeviceWhiteListAuthRulesDTO dto) throws Exception {
        Date nowTime = new Date();
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        params.put("status",dto.getStatus());
        params.put("updateTime",nowTime);
        params.put("operateUserId",dto.getLoginUserId());
        iOperateDeviceWhiteListAuthRulesDao.update(params);
    }

    /**
     * <desc>
     * 根据授权规则id更新授权可用次数
     * </desc>
     *
     * @param dto
     * @Author chengshican
     * @Date 2020-08-16 9:31
     * @Param usedTimes 可用次数  ruleId规则id
     * @Return
     * @Exception
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED,rollbackFor = Exception.class)
    @Override
    public int updateWhiteListAuthRulesByRuleId(OperateDeviceWhiteListAuthRulesDTO dto) throws Exception {

        return iOperateDeviceWhiteListAuthRulesDao.updateWhiteListAuthRulesByRuleId(dto.getUsedTimes(),dto.getRuleId());
    }

    /**
     * <desc>
     * 根据输入的密码获取设备白名单授权文件规则信息集合
     * </desc>
     *
     * @param dto
     * @Author chengshican
     * @Date 2020-08-20 9:18
     * @Param OperateDeviceWhiteListAuthRulesDTO 查询参数
     * @Return
     * @Exception
     */
    @Override
    public List<OperateDeviceWhiteListAuthRulesVO> getWhiteListAuthRulesByList(OperateDeviceWhiteListAuthRulesDTO dto)  throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        return iOperateDeviceWhiteListAuthRulesDao.getWhiteListAuthRulesByList(params);
    }

    /**
     *      校验同一时间内的密码不能重复
     * </desc>
     *
     * @param dto 设备白名单授权文件规则DTO
     * @return
     * @Author Juguang.S
     * @createDate  2020/08/20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED,rollbackFor = Exception.class)
    public Integer checkPassword(OperateDeviceWhiteListAuthRulesDTO dto) throws Exception {
        return iOperateDeviceWhiteListAuthRulesDao.checkPassword(dto.getPassword(),dto.getStartTime(),dto.getEndTime());
    }

}
